/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86136
 * Date: 2024-09-01
 * Time: 16:15
 */
public class MySingleList implements IList{
    static class ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;

    public void createList() {
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);
        ListNode node5 = new ListNode(56);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        head = node1;
    }

    @Override
    public void display() {
        ListNode cur = head;
        while(cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
    public void display2(ListNode newHead) {
        ListNode cur = newHead;
        while(cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
    @Override
    public int size() {
        int len = 0;
        ListNode cur = head;
        while(cur != null) {
            len++;
            cur = cur.next;
        }
        return len;
    }

    @Override
    public boolean contains(int key) {
        if(head == null) {
            return false;
        }
        ListNode cur = head;
        while(cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        while(head == null) {
            head = node;
            return;
        }
        ListNode cur = head;
        while(cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
    }

    @Override
    public void addIndex(int index, int data) {
        int len = size();
        if(index >len || index < 0) {
            System.out.println("下标位置不合法");
            return;
        }
        if(index == 0) {
            addFirst(data);
            return;
        }
        if (index == len) {
            addLast(data);
            return;
        }
        //中间插入
        ListNode cur = head;
        while( index - 1 != 0) {
            cur = cur.next;
            index--;
        }
        ListNode node = new ListNode(data);
        node.next = cur.next;
        cur.next = node;
    }

    @Override
    public void remove(int key) {
        if(head == null) {
            return;
        }
        if(head.val == key) {
            head = head.next;
            return;
        }
        ListNode cur = findNodeOfKey(key);
        if(findNodeOfKey(key) == null) {
            return;
        }
        ListNode del = cur.next;
        cur.next = del.next;
    }
    private ListNode findNodeOfKey(int key) {
        ListNode cur = head;
        while(cur.next != null) {
            if(cur.next.val == key) {
                return cur;
            }else {
                cur = cur.next;
            }
        }
        return null;
    }

    @Override
    public void removeAllKey(int key) {
        if(head == null) {
            return;
        }
        ListNode prev = head;
        ListNode cur = head.next;
        while(cur != null) {
            if(cur.val == key) {
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }
        if(head.val == key) {
            head = head.next;
        }
    }
    @Override
    public void clear() {
        ListNode cur = head;
        while(cur.next != null) {
            ListNode curN = cur.next;
            cur.next = null;
            cur = curN;
        }
        head = null;
    }

    public ListNode reverseList() {
        if(head == null){
            return head;
        }
        ListNode cur = head.next;
        head.next = null;
        while(cur != null) {
            ListNode curN = cur.next;
            cur.next = head;//头插法
            head = cur;
            cur = curN;
        }
        return head;
    }
    public ListNode middleNode() {
        if(head == null) {
            return head;
        }
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

    public int kthToLast(int k) {
        if(head == null) return -1;
        ListNode fast = head;
        ListNode slow = head;
        if(k == 0) return -1;
        while(k -1 > 0) {
            fast = fast.next;
            if(fast == null) return -1;
            k--;
        }
        while(fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow.val;
    }

    //编写代码，以给定值x为基准将链表分割成两部分，
    //所有小于x的结点排在大于或等于x的结点之前
    public ListNode partition(int x) {
        // write code here
        ListNode bs = null;
        ListNode as = null;
        ListNode be = null;
        ListNode ae = null;
        ListNode cur = head;

        while(cur != null) {
            if(cur.val < x) {
                //第一次插入
                if(bs == null) {
                    bs = be = cur;
                }else {
                    //尾插
                    be.next = cur;
                    be = be.next;
                }
            }else {
                if(as == null) {
                    as = ae =cur;
                }else{
                    ae.next = cur;
                    ae = ae.next;
                }
            }
            cur = cur.next;
        }
        if(bs == null) {
            return as;
        }
        be.next = as;
        //不管第二部分的最后一个节点的next是否为空都手动置为空
        if(as != null) {
            ae.next = null;
        }
        return bs;
    }
    //链表的回文结构
    public boolean chkPalindrome() {
        // write code here
        if(head == null) {
            return true;
        }
        ListNode fast = head;
        ListNode slow = head;
        //1.先让slow走到中间节点
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        //2.翻转后面部分
        ListNode cur = slow.next;
        while(cur != null) {
            ListNode curN = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curN;
            // cur = curN;
            // slow = slow.next;//后面找不到slow的next了
        }
        //3.从两端开始比较
        while(head != slow) {
            if(head.val != slow.val) {
                return false;
            }
            //当节点个数为偶数时
            if(head.next == slow) {
                return true;
            }
            head = head.next;
            slow = slow.next;
        }
        return true;
    }

    //判断链表中是否有环
    public boolean hasCycle() {
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null ) {
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow) {
                return true;
            }
        }
        return false;
    }
    //创建带环链表
    public void createLoop() {
        ListNode cur = head;
        while(cur.next != null) {
            cur = cur.next;
        }
        cur.next =head.next.next;
    }

    //给定一个链表的头节点  head ，返回链表开始入环的第一个节点。
    // 如果链表无环，则返回 null
    public ListNode detectCycle(ListNode head) {

        ListNode fast = head;
        ListNode slow = head;
        //先找到相遇点
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow) {
                break;
            }
        }
        if(fast == null || fast.next == null) {
            return null;//没有环
        }

        //slow和fast相遇的节点就是进入环的第一个节点
        slow = head;
        while(fast != slow) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }
}
